/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nesemulator;

import java.io.*;

/**
 *
 * @author ebenj
 */
public class RomData {
     
    public enum Mirrorings {Horizontal,Vertical,FourScreen}
    
    private byte[] fileData;
    public final int PrgRomBanks; //number of 16kb aka 0x4000 program rom banks
    public final int ChrRomBanks; //number of 8kb aka 0x200 character rom banks
    public final Mirrorings Mirroring;
    public final boolean BatteryRamPage60To80;
    public final boolean HalfKilobyteTrainerAtPage70To72;
    public final int MapperNumber4LowBits;
    public final int PrgStart;
    public final int ChrStart;
    
    public RomData(String romPath){
        File f = new File(romPath);
        long numBytes = f.length();
        fileData = new byte[(int)numBytes];
        try{
            FileInputStream in = new FileInputStream(romPath);
            in.read(fileData);
        }
        catch(IOException e){
            
        }
        
        PrgRomBanks = fileData[4];
        ChrRomBanks = fileData[5];  
        
        byte controlByte =  fileData[6];
        Mirroring = NesUtil.BitAt(controlByte,3)==1? Mirrorings.FourScreen: (NesUtil.BitAt(controlByte,0)==0?Mirrorings.Horizontal:Mirrorings.Vertical);
        BatteryRamPage60To80 = NesUtil.BitAt(controlByte,1) == 1;
        HalfKilobyteTrainerAtPage70To72 = NesUtil.BitAt(controlByte,2) == 1;
        MapperNumber4LowBits = NesUtil.BitsAt(controlByte,4,4);
        
        PrgStart = 16;
        ChrStart = PrgStart + 0x4000*PrgRomBanks;
    }
    
}
